<?php
require_once 'BaseController.php';

/**
 * 【SP版】不適切物件情報 コントローラ
 */
class Mobileapi_ClaimentryController extends Mobileapi_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $params = $this->getParameters();
        $this->makeParams($params);
        $this->displayBody2('entry');
    }

    /**
     * メインアクション(post)
     */
    public function postAction()
    {
        $params = $this->getParameters();

        if ($params['complete'] == 'yes')
        {
            $this->displayBody2('complete');
        }
        else
        {
            $this->makeParams($params);
            $this->_data['params'] = $params;
            if ($this->checkParams($params) == 1)
            {
                $this->displayBody2('error');
            }
            else
            {
                $this->displayBody2('confirm');
            }
        }
    }

    private function makeParams($params)
    {
        $this->_data["BukkenCd"] = $params["BukkenCd"];
        $this->_data["ShopCd"] = $params["ShopCd"];

        $model = new Apm_Model_Base();

        $sql = "select M05_TENPO_NAME from M05TENPO_INFO where M05_TENPO_CD=:ShopCd";
        $dat = $model->dbGetData($sql, array(
            "ShopCd" => $params["ShopCd"]
        ));
        $this->_data["shop_name"] = $dat["M05_TENPO_NAME"];

        $sql = "select T03_TATEMONO_NAME,T03_HEYA_BANGOU from T03WEB_HEYA where T03_HEYA_CD=:BukkenCd";
        $dat = $model->dbGetData($sql, array(
            "BukkenCd" => $params["BukkenCd"]
        ));
        $this->_data["bukken_name"] = $dat["T03_TATEMONO_NAME"];
        $this->_data["heya_bangou"] = $dat["T03_HEYA_BANGOU"];
    }

    private function checkParams($params)
    {
        $error = 0;

        $key = array();
        $key[] = 'kanji_sei';
        $key[] = 'kanji_mei';
        foreach ($key as $k)
        {
            if ($this->checkZenkaku($params[$k]))
            {
                $this->_data['name_error'] = 1;
                $this->_data[$k . '_error'] = "error";
                $error = 1;
            }
        }
        $key = array();
        $key[] = 'kana_sei';
        $key[] = 'kana_mei';
        foreach ($key as $k)
        {
            if ($this->checkZenkakuKana($params[$k]))
            {
                $this->_data['name_error'] = "error";
                $this->_data[$k . '_error'] = "error";
                $error = 1;
            }
        }

        if (empty($params['hojinmei']))
        {
            $this->_data['hojinmei_error'] = "error";
            $error = 1;
        }
        if (empty($params['jusho']))
        {
            $this->_data['jusho_error'] = "error";
            $error = 1;
        }

        if ($this->checkMail($params['mail']))
        {
            $this->_data['mail_error'] = "error";
            $this->_data['renraku_error'] = "error";
            $error = 1;
        }
        if (!preg_match("/^[0-9]+$/u", $params['tel']))
        {
            $this->_data['tel_error'] = "error";
            $this->_data['renraku_error'] = "error";
            $error = 1;
        }
        if (empty($params['mail']) && empty($params['tel']))
        {
            $this->_data['renraku_empty_error'] = "error";
            $this->_data['renraku_error'] = "error";
            $error = 1;
        }

        $checks = 0;
        for ($i = 1; $i <= 5; $i++)
        {
            if ($params['check' . $i] == 'on')
            {
                $checks++;
            }
        }
        if ($checks == 0)
        {
            $this->_data['checks_error'] = "error";
            $error = 1;
        }
        else if (!empty($params['check5']) && empty($params['check5_text']))
        {
            $this->_data['check5_error'] = "error";
            $error = 1;
        }

        if (empty($params['shosai']) or strlen($params['shosai']) > 400)
        {
            $this->_data['shosai_error'] = "error";
            $error = 1;
        }

        return $error;
    }

}
?>
